//CircularBuffer.c
#include "CircularBuffer.h"

#include <stdlib.h>

typedef struct CircularBufferStruct {
	int size;
	int inIndex;
	int outIndex;
	INT16* array;
} CircularBufferStruct;


CircularBuffer CircularBuffer_Init(INT16* buffer, int size)
{
	CircularBuffer buff = malloc(sizeof(CircularBufferStruct));
	buff->size = size;	
	buff->inIndex = 0;
	buff->outIndex = 0;
	buff->array = buffer;
	
	return buff;
}	

void CircularBuffer_Destroy(CircularBuffer b)
{
	free(b);
}	

BOOL CircularBuffer_Add(CircularBuffer buff, int value)
{
	int index = buff->inIndex;
	
	++buff->inIndex;
	if(buff->inIndex >= buff->size)
	{
		buff->inIndex = 0;
	}	
	buff->array[index] = value;
	
	return TRUE;
}
	
INT16 CircularBuffer_Get(CircularBuffer buff)
{
	int index = buff->outIndex;
	
	++buff->outIndex;
	if(buff->outIndex >= buff->size)
	{
		buff->outIndex = 0;
	}	
	return buff->array[index];
}	
